package com.android.test.activity;

import com.android.test.R;
import com.android.test.dialog.WelcomeDialog;
import com.android.test.util.GlobalUtil;
import com.android.test.util.SharedPreferencesUtil;
import com.android.test.view.AnimTextView;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

public class SplashActivity extends Activity {

    private static final int MSG_INIT = 1;
    private static final int MSG_PROGRESS = 2;
    private Context context;
    private AnimTextView line1;
    private AnimTextView line2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	context = this;

	// Set window
	requestWindowFeature(Window.FEATURE_NO_TITLE);
	getWindow().setFormat(PixelFormat.RGBA_8888);
	getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
		WindowManager.LayoutParams.FLAG_FULLSCREEN);

	setContentView(R.layout.activity_splash);
	line1=(AnimTextView) findViewById(R.id.line1);
	line2=(AnimTextView) findViewById(R.id.line2);
	

   line1.setDivideCount(3);
   line1.setTextForView(0, "孩儿");
   line1.setTextForView(1, "立志");
   line1.setTextForView(2, "出乡关");

   line1.getChildViewAnim(1).setAnimType(AnimTextView.ANIM_TYPE_SCALE);
   
   line2.setDivideCount(3);
   line2.setTextForView(0, "學不");
   line2.setTextForView(1, "成名");
   line2.setTextForView(2, "誓不還");

   line2.getChildViewAnim(1).setAnimType(AnimTextView.ANIM_TYPE_SCALE);
	
	mSplashHandler.sendMessageDelayed(
		mSplashHandler.obtainMessage(MSG_PROGRESS, 0), 100);

    }

    private Handler mSplashHandler = new Handler() {
	public void handleMessage(Message msg) {
	    switch (msg.what) {
	    case MSG_INIT:
		break;
	    case MSG_PROGRESS:
		int progress = (Integer) msg.obj;
		if (progress < 5) {
		    ((ImageView) findViewById(R.id.progress1 + progress))
			    .setImageResource(R.drawable.progress);
		    mSplashHandler.sendMessageDelayed(mSplashHandler
			    .obtainMessage(MSG_PROGRESS, ++progress), 800);
		} else {
		    /*
		     * Dialog d = new WelcomeDialog(context); d.show();
		     */

		    Intent intent = new Intent();
		    intent.setClass(SplashActivity.this, WelcomeActivity.class);
		    startActivity(intent);
		    finish();

		}
		break;
	    }
	};
    };

    @Override
    public void onResume() {
	super.onResume();
    }

    @Override
    public void onPause() {
	super.onPause();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
	if (keyCode == KeyEvent.KEYCODE_BACK) {
	    return true;
	}
	return super.onKeyDown(keyCode, event);
    }

    private void checkIsFirstStart() {
	int versionCode_SP = SharedPreferencesUtil.getInt(SplashActivity.this,
		"testApp_versioncode", 0);
	int versionCode_CurrentAPP = getVersionCode();

	if (versionCode_SP == 0) {
	    SharedPreferencesUtil.putInt(SplashActivity.this,
		    "testApp_versioncode", versionCode_CurrentAPP);
	    SharedPreferencesUtil.putBoolean(SplashActivity.this,
		    "first_started", true);
	} else {
	    if (versionCode_SP < versionCode_CurrentAPP) {
		SharedPreferencesUtil.putInt(SplashActivity.this,
			"testApp_versioncode", versionCode_CurrentAPP);
		SharedPreferencesUtil.putBoolean(SplashActivity.this,
			"first_started", true);
	    }
	}
    }

    private int getVersionCode() {
	PackageInfo packageInfo = GlobalUtil.getPackageInfo(
		getPackageManager(), "com.android.test");
	int versionCode = 0;
	if (packageInfo != null) {
	    versionCode = packageInfo.versionCode;
	}
	return versionCode;
    }
}
